package com.tbycoder.tby.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tbycoder.tby.entity.IposQuestion;
import com.tbycoder.tby.entity.Request.CommonIdRequest;
import com.tbycoder.tby.entity.Request.PublishRequest;
import com.tbycoder.tby.entity.Vo.GetQuestionVo;
import com.tbycoder.tby.excption.BizException;
import com.tbycoder.tby.mapper.IposQuestionMapper;
import com.tbycoder.tby.service.IIposQuestionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.swing.plaf.BorderUIResource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tby
 * @since 2021-05-13
 */
@Service
public class IposQuestionServiceImpl extends ServiceImpl<IposQuestionMapper, IposQuestion> implements IIposQuestionService {

    @Resource
    IposQuestionMapper iposQuestionMapper;
    @Override
    public GetQuestionVo getAllQuestion() {
        List<String> question = new ArrayList<>();
        List<String> answer = new ArrayList<>();
        List<IposQuestion> iposQuestions = iposQuestionMapper.selectList(Wrappers.lambdaQuery(IposQuestion.class));
        iposQuestions.forEach((questions)->
                question.add(questions.getQuestionContent()));
        iposQuestions.forEach((question1)->
                answer.add(question1.getQuestionAnswer()));
        return GetQuestionVo.builder()
                .answers(answer)
                .questions(question)
                .build();
    }

    @Override
    public boolean deleteQuestion(CommonIdRequest question) {
        iposQuestionMapper.delete(Wrappers.lambdaQuery(IposQuestion.class).eq(IposQuestion::getQuestionContent,question.getId()));
        return true;
    }

    @Override
    public void questionAndAnswer(PublishRequest publishRequest) {
        if(publishRequest.getTopic().equals("")){
            throw new BizException("未填写主题，请填写");
        }
        if(publishRequest.getContext().equals("")){
            throw new BizException("未填写内容，请填写");
        }
        iposQuestionMapper.insert(
                IposQuestion.builder()
                        .questionContent(publishRequest.getTopic())
                        .questionAnswer(publishRequest.getContext())
                        .build()
        );
    }
}
